package frontend.pages.reports.workperemployee;

import backend.model.worklog.WorkLog;
import frontend.model.reports.ReportInputUIModel;
import frontend.pages.reports.base.AbstractReportForm;
import frontend.pages.reports.base.AbstractWorklogReportPanel;
import frontend.pages.worklog.daily.DefaultWorkLogTableTemplate;
import frontend.reports.XLSFileRowCreator;
import shared.util.CalendarUtils;
import org.apache.wicket.Localizer;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class WorkPerEmployeeReportPanel extends AbstractWorklogReportPanel {
    private static String RESOURCE_KEY_REPORT_FILE_NAME = "report.fileName.workPerEmployee";

    private WorkPerEmployeeReportTableTemplate workPerEmployeeReportTableTemplate;
    private ReportInputUIModel uiModel;

    public WorkPerEmployeeReportPanel(String id) {
        super(id);
    }

    @Override
    protected AbstractReportForm getReportForm() {
        uiModel = new ReportInputUIModel();
        return new WorkPerEmployeeReportForm("reportForm", uiModel, reportResults, reportResultsContainer);
    }

    @Override
    protected DefaultWorkLogTableTemplate getTableTemplate() {
        workPerEmployeeReportTableTemplate = new WorkPerEmployeeReportTableTemplate();
        return workPerEmployeeReportTableTemplate;
    }

    @Override
    protected String getReportFileName() {
        return Localizer.get().getString(RESOURCE_KEY_REPORT_FILE_NAME, null);
    }

    @Override
    protected List<String> getReportFileHeader() {
        return workPerEmployeeReportTableTemplate.getHeader();
    }

    @Override
    protected Map<String, String> getReportDetails() {
        Map<String, String> reportDetails = super.getReportDetails();
        if (uiModel != null) {
            reportDetails.put(Localizer.get().getString(AbstractReportForm.RESOURCE_KEY_FIELD_LABEL_START_DATE, null),
                    CalendarUtils.formatDate(uiModel.getStartDate()));
            reportDetails.put(Localizer.get().getString(AbstractReportForm.RESOURCE_KEY_FIELD_LABEL_END_DATE, null),
                    CalendarUtils.formatDate(uiModel.getEndDate()));
            reportDetails.put(Localizer.get().getString(WorkPerEmployeeReportForm.PAGES_WORK_PER_EMPLOYEE_REPORT_FORM_EMPLOYEE, null),
                    uiModel.getEmployee().getDisplayLabel());
        }
        return reportDetails;
    }

    @Override
    protected String getReportName() {
        return Localizer.get().getString("menu.reports.items.workLogPerEmployee", null);
    }

    @Override
    protected List<Object[]> getReportFileData() {
        List<Object[]> reportFileData = new LinkedList<Object[]>();
        for (WorkLog workLog : reportResults) {
            XLSFileRowCreator rowCreator = new XLSFileRowCreator(13);
            Object[] row = rowCreator.newRow();
            rowCreator.addDateCell(row, 0, workLog.getDate());
            rowCreator.addStringCell(row, 1, workLog.getIdentificationCard());
            rowCreator.addDBModelCell(row, 2, workLog.getOccupation());
            rowCreator.addDBModelCell(row, 3, workLog.getTeam());
            rowCreator.addDBModelCell(row, 4, workLog.getForeman());
            rowCreator.addDBModelCell(row, 5, workLog.getProject());
            rowCreator.addDBModelCell(row, 6, workLog.getSfi());
            rowCreator.addDBModelCell(row, 7, workLog.getActivity());
            rowCreator.addDBModelCell(row, 8, workLog.getDepartment());
            rowCreator.addDBModelCell(row, 9, workLog.getWorkplace());
            rowCreator.addDBModelCell(row, 10, workLog.getWorkDetail());
            rowCreator.addDBModelCell(row, 11, workLog.getShift());
            rowCreator.addHoursCell(row, 12, workLog.getHours());
            reportFileData.add(row);
        }
        return reportFileData;
    }
}
